package com.itzxx.huazhun.ui.user

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.chad.library.adapter4.loadState.trailing.TrailingLoadStateAdapter
import com.itzxx.huazhun.databinding.FragmentUserManagementBinding
import com.itzxx.huazhun.ui.MainViewModel
import com.itzxx.huazhun.view.adapter.CustomLoadMoreAdapter
import com.itzxx.library_base_ktx.base.fragment.BaseVmDbFragment
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter

class UserManagementFragment :
    BaseVmDbFragment<UserManagementViewModel, FragmentUserManagementBinding>() {

    //获取共享ViewModel
    private val mainSharedViewModel: MainViewModel by activityViewModels()

    private lateinit var userListAdapter: UserListAdapter
    private lateinit var helper: QuickAdapterHelper
    private lateinit var loadMoreAdapter: CustomLoadMoreAdapter


    override fun initView(savedInstanceState: Bundle?) {
        initRecyclerView()

    }

    private fun initRecyclerView() {
        userListAdapter = UserListAdapter()
        loadMoreAdapter = CustomLoadMoreAdapter()
        loadMoreAdapter.setOnLoadMoreListener(object : TrailingLoadStateAdapter.OnTrailingListener {
            override fun onLoad() {
                // 执行加载更多的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun onFailRetry() {
                // 加载失败后，点击重试的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun isAllowLoading(): Boolean {
                return true
            }
        })
        helper = QuickAdapterHelper.Builder(userListAdapter)
            .setTrailingLoadStateAdapter(loadMoreAdapter)
            .build()
        helper.trailingLoadState = LoadState.None
        mDatabind.rvList.addLinearAdapter(helper.adapter)

    }

    override fun lazyLoadData() {
    }

    override fun createObserver() {
        mViewModel.run {
            listDataUiStateLiveData.observe(this@UserManagementFragment) {
                if (it.isRefresh) {
                    userListAdapter.submitList(it.listData)
                } else {
                    userListAdapter.addAll(it.listData)
                }

                if (it.hasMore) {
                    helper.trailingLoadState = LoadState.NotLoading(false)
                } else {
                    loadMoreAdapter.setFirstNotFullScreen(it.isRefresh)
                    helper.trailingLoadState = LoadState.NotLoading(true)
                }
            }
        }

        mainSharedViewModel.run {
            userInfoLiveData.observe(this@UserManagementFragment) {
                mDatabind.userInfo = it
            }

            userPermissionLiveData.observe(this@UserManagementFragment) {
                mDatabind.userPermissionInfo = it
            }
        }
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }
}